{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 环境准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%sh\n",
    "\n",
    "conda remove -n ipex-llm-xpu --all -y\n",
    "conda create -n ipex-llm-xpu python=3.11 libuv -y\n",
    "conda activate ipex-llm-xpu\n",
    "\n",
    "pip install --pre --upgrade numpy==1.26.4 torch==2.1.0a0 torchvision==0.16.0a0 torchaudio==2.1.0a0 ipex-llm[xpu] --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/cn/\n",
    "pip install modelscope==1.22.3 openai==1.55.3 httpx==0.27.2 addict==2.4.0 simplejson==3.19.3 python-dateutil==2.9.0.post0 sortedcontainers==2.4.0\n",
    "pip install typer==0.15.1 fastapi==0.115.7 uvicorn==0.34.0 sse-starlette==2.2.1 omegaconf==2.3.0 datasets==3.2.0 soundfile==0.13.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# XPU 使用案例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模型对话"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "requests.get(url=\"http://localhost:8030/v1/models\").json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "from IPython import display\n",
    "\n",
    "base_url = \"http://localhost:8030/v1/\"\n",
    "client = OpenAI(api_key=\"EMPTY\", base_url=base_url)\n",
    "\n",
    "messages = [{\n",
    "                \"role\": \"user\",\n",
    "                \"content\": \"请介绍一下你自己\"\n",
    "            }]\n",
    "use_stream=False\n",
    "\n",
    "response = client.chat.completions.create(\n",
    "        model=\"glm-4\",\n",
    "        messages=messages,\n",
    "        stream=use_stream,\n",
    "        max_tokens=4096,\n",
    "        temperature=0.7,\n",
    "        presence_penalty=1.2,\n",
    "        top_p=0.8,\n",
    "    )\n",
    "\n",
    "if use_stream:\n",
    "    answer = \"\"\n",
    "    for chunk in response:\n",
    "        answer += chunk.choices[0].delta.content\n",
    "        display.clear_output(wait=True)\n",
    "        print(answer)\n",
    "else:\n",
    "    print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 语音生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, json\n",
    "\n",
    "url=\"http://localhost:8030/tts/speecht5/audio/speech\"\n",
    "\n",
    "headers = {'Content-Type': 'application/json; charset=utf-8', 'accept': 'application/json'}\n",
    "data=json.dumps({'input': 'Hello, my dog is cooler than you!'})\n",
    "\n",
    "response = requests.post(url=url, headers=headers, data=data)\n",
    "\n",
    "from IPython.display import Audio\n",
    "Audio(url=response.json()[\"data\"][\"url\"], rate=16000)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ipex-llm-xpu",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
